# coding:utf-8

from flask_wtf import FlaskForm
from wtforms import (
    StringField, PasswordField, BooleanField, SubmitField,
    FloatField, IntegerField, FileField
)
from wtforms.validators import DataRequired, EqualTo, Optional, NumberRange
from flask_wtf.file import FileAllowed

from ..forms import NonValidatingSelectField
from .constant import CaptchaSmsChannel, AdminRole


class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('保持登录')
    submit = SubmitField('登录')


class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('原密码', validators=[DataRequired()])
    password = PasswordField('新密码', validators=[
        DataRequired(), EqualTo('password2', message='密码不一致')])
    password2 = PasswordField('确认密码', validators=[DataRequired()])
    submit = SubmitField('确认提交')


class AdminUserForm(FlaskForm):
    """
    新建/编辑管理员表单
    """

    username = StringField('账号', validators=[DataRequired()])
    nickname = StringField('昵称', validators=[DataRequired()])
    new_pwd = PasswordField(
        '重置密码(不填则维持原密码)', validators=[Optional()])
    seven_moor_account = StringField('七陌账号', validators=[Optional()])
    seven_moor_password = StringField('七陌密码', validators=[Optional()])
    role_id = NonValidatingSelectField(
        '账号角色',
        choices=AdminRole.to_options(),
        validators=[Optional()],
        coerce=int,
    )
    is_deleted = BooleanField('是否禁用')
    submit = SubmitField('保存')


class ConfForm(FlaskForm):
    """
    created by chenhj at 2017/8/31
    管理员相关参数配置
    """
    # to订单生成的触发概率
    to_rate = FloatField('To概率(α, 0-1)', default=0, validators=[NumberRange(0, 1, 'α只能在0-1之间')])
    protocol_version = StringField('协议版本', default='')
    use_sms_win_bid_notify = BooleanField('使用短信发送中标提醒', default=False)
    outbound_use_virtual_number = BooleanField('自动外呼使用虚拟号', default=False)
    bid_user_limit = IntegerField('中标人数(非维修)', default=1)
    maintain_user_limit = IntegerField('中标人数(维修)', default=1)
    watering_index = FloatField('首发浇水全局系数', default=0)
    second_watering_index = FloatField('二次发布浇水全局系数', default=0)
    other_watering_index = FloatField('多次发布浇水全局系数', default=0)

    captcha_sms_channel = NonValidatingSelectField(
        '验证码短信通道',
        choices=CaptchaSmsChannel.to_options(),
        validators=[Optional()]
    )

    today_demand_count = FloatField('今日订单数系数', default=1.0)

    submit = SubmitField('保存')


class AppVersionForm(FlaskForm):
    """
    created by chenhj at 2018/5/8

    """
    android_version = StringField('安卓版本', default='')
    ios_version = StringField('iOS版本', default='')
    android_file = FileField(
        '安卓apk文件',
        validators=[Optional(), FileAllowed(['apk'])]
    )
    submit = SubmitField('保存')
